#include <iostream>
#include <algorithm>
#include <list>
using namespace std;
int main()
{
	list<int> l1, l2;
	int n; cin >> n;
	while (n--)
	{
		int tmp; cin >> tmp;
		l1.push_back(tmp);
	}
	cin >> n;
	while (n--)
	{
		int tmp; cin >> tmp;
		l2.push_back(tmp);
	}
	cout << "head-->";
	for (const auto& e1 : l1)
	{
		for (const auto& e2 : l2)
		{
			if (e1 == e2)
			{
				cout << e1 << "-->";
				break;
			}
		}
	}
	cout << "tail";
	return 0;
}